package dilmaj.client;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import dilmaj.client.AddAreasPanel.CloseHandler;
import dilmaj.shared.CommentVO;
import dilmaj.shared.TermVO;

public class CommentPanel extends PopupPanel {
	TermVO termVO;
	TermServiceAsync termSvc = GWT.create(TermService.class);
	CommentServiceAsync commentSvc = GWT.create(CommentService.class);
	UserServiceAsync userSvc = GWT.create(UserService.class);
	VerticalPanel mainPanel=new VerticalPanel();
	TextArea commentBox=new TextArea();
	FlexTable commentsTable=new FlexTable();
	Button closeButton=new Button("Close");
	Button addButton=new Button("Add");
	TextBox userBox=new TextBox();

	public CommentPanel(TermVO termVO, String username, Long userid) {
		this.termVO=termVO;
		CloseHandler closeHandler=new CloseHandler(this);
		closeButton.addClickHandler(closeHandler);
		addButton.addClickHandler(new AddHandler());
		mainPanel.add(userBox);
		mainPanel.add(commentBox);
		mainPanel.add(addButton);
		mainPanel.add(commentsTable);
		mainPanel.add(closeButton);
		
		commentSvc.getAllComments(termVO.getComments(), loadCallback);
		
		if (username!=null && username.compareTo("")!=0) {
			userBox.setText(username);
			userBox.setEnabled(false);
			userBox.setTitle(userid+"");
		}
		
		add(mainPanel);
	}

	
	
	class AddHandler implements ClickHandler {
		public void onClick(ClickEvent event) {
			CommentVO commentVO=new CommentVO(commentBox.getText());
			if (userBox.getTitle()!=null && userBox.getTitle().compareTo("")!=0) {
				commentVO.setUserID(Long.valueOf(userBox.getTitle()));
				commentVO.setUserName(userBox.getText());
			} else {
				commentVO.setAnyUser(userBox.getText());
			}

		    if (commentSvc == null) {
			      commentSvc = GWT.create(CommentService.class);
		    }
		    commentSvc.addComment(commentVO, addCallback);
		}
	}
	
	class CloseHandler implements ClickHandler {
		private CommentPanel owner;
		
		public CloseHandler(CommentPanel owner) {
			this.owner=owner;
		}
		
		public void onClick(ClickEvent event) {
			owner.hide();
		}
	}
	
	AsyncCallback<List<CommentVO>> loadCallback = new AsyncCallback<List<CommentVO>>() {
		public void onFailure(Throwable caught) {
			        // TODO: Do something with errors.
		}
	
		public void onSuccess(List<CommentVO> result) {
			Iterator iterator=result.iterator();
			int row=0;
			while (iterator.hasNext()) {
				CommentVO commentVO=(CommentVO)iterator.next();
				commentsTable.setText(row, 0, commentVO.getText());
				
				String userName=commentVO.getUsername();
				String anyUser=commentVO.getAnyUser();
				
				if (userName==null) {
					commentsTable.setText(row, 1, commentVO.getAnyUser());
				} else if (userName.compareTo("")==0) {
					commentsTable.setText(row, 1, commentVO.getAnyUser());
				} else {
					commentsTable.setText(row, 2, commentVO.getUsername());
				}
				
				row++;
			}
		}
	};

	AsyncCallback<TermVO> updateCallback = new AsyncCallback<TermVO>() {
		public void onFailure(Throwable caught) {
			        // TODO: Do something with errors.
		}
	
		public void onSuccess(TermVO result) {
			int i=1;
		}
	};

	AsyncCallback<CommentVO> addCallback = new AsyncCallback<CommentVO>() {
		public void onFailure(Throwable caught) {
			        // TODO: Do something with errors.
		}
	
		public void onSuccess(CommentVO result) {
		    termVO.addComment(result);
		    if (termSvc == null) {
			      termSvc = GWT.create(TermService.class);
		    }
		    termSvc.update(termVO, updateCallback);

			int row=commentsTable.getRowCount();
			commentsTable.setText(row, 0, result.getText());
			String userName=result.getUsername();
			String anyUser=result.getAnyUser();
			
			if (userName==null) {
				commentsTable.setText(row, 1, result.getAnyUser());
			} else if (userName.compareTo("")==0) {
				commentsTable.setText(row, 1, result.getAnyUser());
			} else {
				commentsTable.setText(row, 2, result.getUsername());
			}
		}
	};
}
